package cn.huang.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import cn.huang.constant.MessageConstant;
import cn.huang.entity.PageResult;
import cn.huang.entity.QueryPageBean;
import cn.huang.entity.Result;
import cn.huang.pojo.Address;
import cn.huang.service.AddressService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    private AddressService addressService;

    /**
     * 添加体检项目
     * @param address
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        try {
            addressService.add(address);
            return new Result(true, "新增地址成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "新增地址失败");
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = addressService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 根据id删除检查项
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            addressService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @RequestMapping("/findAddress")
    public Result findAddress(Integer id) {
        try {
            List<String> list2 = new ArrayList<>();
            //Object以及子类
            Address address = addressService.findAddressById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, address);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
         }
    }

    /**
     * 更新检查项
     * @param address
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Address address) {
        try {
            addressService.update(address);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
    /**
     * 更新检查项
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Address> allData = addressService.findAll();
            List<Map<String,Object>> points = new ArrayList<Map<String,Object>>();
            if (allData != null && allData.size() > 0){
                for (Address address : allData) {
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("lng",address.getLongitude());
                    map.put("lat",address.getLatitude());
                    map.put("address",address.getAddress());
                    points.add(map);
                }
            }

            return new Result(true,"查询地址列表成功！",points);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询地址列表失败！");
        }
    }
    //手机端查询所有地址信息
    @RequestMapping("/findAll4Mobile")
    public Result findAll4Mobile() {
        try {
            List<Address> addressList = addressService.findAll();
            return new Result(true,"查询地址列表成功！",addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询地址列表失败！");
        }
    }



}
